<div id="jxEmpDiv" style="width: 100%;height: 100%">
    <div id="jxEmpTableToolbar" style="background-color: #CCE6F1">
        <div style="background-color: #CCE6FF;height: 30px;text-align: center;line-height: 30px">
            <form id="jxEmpForm">
                <div style="float: left">
                <input class="easyui-textbox" name="empName" data-options="label:'姓名<i class=&quot;fa fa-user-circle fa-fw\&quot;></i>',labelWidth:50,buttonAlign:'left'">
                <input class="easyui-textbox" name="empCode" data-options="label:'工号<i class=&quot;fa fa-asterisk fa-fw\&quot;></i>',labelWidth:50,buttonAlign:'left'">
                <input id="jxEmpFormPkOrg" class="easyui-combobox" style="width: 230px"  name="pkOrg" data-options="
                label:'组织<i class=&quot;fa fa-user-circle fa-fw\&quot;></i>',
                labelWidth:50,
                buttonAlign:'left',
                valueField: 'pkOrg', textField: 'name',panelHeight:'auto',panelWidth:'180',
                data:[{pkOrg:'0001O110000000001HET',name:'澳宝化妆品（惠州）有限公司'},{pkOrg:'0001O110000000001HGC',name:'惠州兆婷化妆品有限公司'}]">
                <input id="jxEmpFormPkDept"  name="pkDept" style="width: 180px" data-options="
                label:'部门<i class=&quot;fa fa-address-book-o fa-fw\&quot;></i>',
                labelWidth:50,
                panelHeight:'auto',
                buttonAlign:'left'">
                <input id="jxEmpFormPkPost" class="easyui-textbox" name="postId" data-options="label:'岗位<i class=&quot;fa fa-anchor fa-fw\&quot;></i>',labelWidth:50,buttonAlign:'left'">
                <input class="easyui-combobox" name="empGroup" data-options="
                label:'班组<i class=&quot;fa fa-asl-interpreting fa-fw\&quot;></i>',
                labelWidth:50,
                buttonAlign:'left',
                valueField: 'empGroupId',
                textField: 'empGroup',
                panelHeight:'auto',
                data:[{empGroupId: 'A', empGroup: 'A'},{empGroupId: 'B', empGroup: 'B'},{empGroupId: 'C',empGroup: 'C'},{empGroupId: 'D', empGroup: 'D'}]">
                </div>
                <div style="float: right">
                <a class="easyui-linkbutton c4" onclick="queryJxempData()" lick="" style="width: 90px;">查询</a>
                </div>
            </form>
        </div>
        <div style="clear: both"></div>
        <div style="background-color: #efefef;margin-top: 10px;padding: 3px">
        <div style="width: 460px;float: left">
            <a class="easyui-linkbutton c6" data-options="plain:true"  onClick="javaScript:$('#jxEmpTable').edatagrid('addRow',0)"><i class="fa fa-plus-square fa-fw" aria-hidden="true"></i>添加员工</a>
        </div>
        <div style="width: 280px;float: right;text-align: right">
            <a class="easyui-linkbutton c5" data-options="plain:true"  onclick="deleteJxEmpTableRow()"><i class="fa fa-trash fa-fw" aria-hidden="true"></i>删除</a>
            <a class="easyui-linkbutton" data-options="plain:true">|</a>
            <a class="easyui-linkbutton c1" data-options="plain:true" onclick="javaScript:$('#jxEmpTable').edatagrid('saveRow')"><i class="fa fa-floppy-o fa-fw" aria-hidden="true"></i>保存</a>
        </div>
        <div style="clear:both"> </div>
        </div>
    </div>
    <!--员工表格-->
    <div style="width:95%;margin: 0 auto">
        <table id="jxEmpTable"></table>
    </div>
    <script th:inline="none">
        //调用分辨率方法
        getCostsystemFbl(systemFblW,systemFblH);
        //所有部门的信息，用于格式化显示部门
        var pkDeptAll = commonAjax("post", "/jxEmp/getAllJxDept")
        //所有岗位的信息，用于格式化显示岗位名称
        var post = commonAjax("get","/jxEmp/getjxPostAll");
        //用于格式化所属科长、所属组长、所属主管
        var empAll=commonAjax("post","/jxEmp/selectAllJxEmp");
        //点击查询获取刷新员工表
        function queryJxempData(){
            var param=$("#jxEmpForm").serialize()
            JxEmpTableInit("/jxEmp/selectJxEmp?"+param);
        }

        //打开页面执行方法
        JxEmpTableInit("/jxEmp/selectJxEmp")
        //表初始化方法
        function                                                                                                                                                                                                                                                                                                                                                    JxEmpTableInit(url){
            $("#jxEmpTable").edatagrid({
                scrollbarSize:0,
                toolbar:"#jxEmpTableToolbar",
                fitColumns:true,
                height:window.costsystemFblH,
                columns:jxEmpTableColumns(),
                url:url,
                pagination:true,
                pageSize:30,
                pageList:[10,20,30,40,50],
                updateUrl:"/jxEmp/updateJxEmp",
                saveUrl:"/jxEmp/saveJxEmp",
                onEdit: function (index, row) {
                    jxEmpTableEdit(index, row)
                },
                onAdd:function (index, row) {
                    jxEmpTableEdit(index, row)
                },
                onSave:function (index,row) {
                    //若row中带id证明是数据库有，没有则是新增
                    var status = row.status;
                    if(status){
                        if(status!=200){
                            $('#jxEmpTable').datagrid('deleteRow', index);
                        }
                        windowsOperation(null, null, {status:row.status,msg:row.msg});
                    }

                }
            })
        }

        //表中的字段从后台获取
        function jxEmpTableColumns() {
            //从数据库中获取员工表的columns
            var columns=commonAjax("get","/easyui/datagrid/getEasyuiDatagridColumn",{typeName:"jx_emp_column"})
            //对editor_options_onchange的字符串进行转换成fun类型
            columns.forEach(function (val) {
                if (val){
                    val.forEach(function (v) {
                        var formatter = v.formatter;
                        if (formatter){
                            v.formatter= window[formatter]
                        }
                        var editor = v.editor;
                        if (editor){
                            var options = editor.options;
                            var onChange = options.onChange;
                            if (onChange){
                                options.onChange= window[onChange]
                            }
                        }
                    })
                }
            })
            //往字段中添加edit
            columns[0].forEach(function (value) {
                var field = value.field;
                //姓名
                if (field=="empName"){
                    //添加编辑对象
                    value.editor={type:"textbox",options:{required: true}}
                }
                //工号
                if (field=="empCode"){
                    //添加编辑对象
                    value.editor={type:"textbox",options:{required: true}}
                }
                //组织
                if (field=="pkOrg") {
                    //添加编辑对象
                    value.editor = {type: "combobox", options: {valueField: 'pkOrg',limitToList:true, textField: 'name',panelHeight:'auto'}}
                    //添加格式化
                    value.formatter = function (value, row, index) {
                        switch (value) {
                            case "0001O110000000001HET":
                                value = "澳宝化妆品（惠州）有限公司";
                                break;
                            case "0001O110000000001HC5":
                                value = "澳寶化粧品（香港）有限公司";
                                break;
                            case "0001O110000000001HGC":
                                value = "惠州兆婷化妆品有限公司";
                                break;
                            case "0001O110000000001HJE":
                                value = "品木丝序园林绿化建筑工程（惠州）有限公司";
                                break;
                            case "0001O110000000001HHM":
                                value = "澳寶國際貿易有限公司";
                        }
                        return value;
                    }
                }
                //班组
                if (field == "empGroup") {
                    //添加编辑对象
                    value.editor = {
                        type: "combobox",
                        options: {
                            required: true,limitToList:true,
                            valueField: 'empGroupId',
                            textField: 'empGroup',
                            panelHeight:'auto',
                            data: [{empGroupId: "A", empGroup: "A"}, {empGroupId: "B", empGroup: "B"}, {
                                empGroupId: "C",
                                empGroup: "C"
                            }, {empGroupId: "D", empGroup: "D"}]
                        }
                    }
                }
                //岗位
                if (field == "postId") {
                    //添加编辑对象
                    value.editor = {
                        type: "combobox",
                        options: {
                            required: true,
                            limitToList:true,
                            valueField: 'id',
                            textField: 'name',
                            panelHeight:'auto'
                        }
                    }
                    //添加格式化
                    value.formatter = function (value, row, index) {
                        var resultPost = "";
                        post.forEach(function (p) {
                            if (p.id==value){
                                resultPost=p.name;
                                return ;
                            }
                        })
                        return resultPost;
                    }
                }
                if(field=="empEntry") {
                    value.editor = {
                        "type": "combobox",
                        "options": {
                            valueField: "empEntry",
                            textField: "entryText",
                            panelHeight: "auto",
                            data: [{empEntry: "-1", entryText: "正式员工"}, {
                                empEntry: "1",
                                entryText: "入职一个月"
                            }, {empEntry: "2", entryText: "入职二个月"}, {empEntry: "3", entryText: "入职三个月"}]
                        }
                    }
                    value.formatter = function (value, row, index) {
                        switch (value) {
                            case 1:
                                return "入职一个月";
                                break;
                            case 2:
                                return "入职二个月";
                                break;
                            case 3:
                                return "入职三个月";
                                break;
                            default:
                                return "正式员工";
                        }
                    }
                }
            })
            return columns;
        }
        //删除方法  ：edatagrid组件的删除方法会报错，不知道啥原因
        function deleteJxEmpTableRow() {
            //获取选中的行
            var empRow = $('#jxEmpTable').datagrid('getSelected');
            var id=empRow.id;
            if (empRow){
                if (id) {
                    $.messager.confirm('确认', '您确认想要删除<span style="color:red;">' + empRow.empName + '</span>用户吗？', function (r) {
                        if (r) {
                            $.post({
                                url: "/jxEmp/deleteJxEmp",
                                data: {id:empRow.id},
                                success: function (data) {
                                    if (data.status==200){
                                        //删除选中的行：参数：方法名，参数二：根据当前的行获取索引
                                        $('#jxEmpTable').datagrid('deleteRow', $('#jxEmpTable').datagrid('getRowIndex',empRow));
                                    }
                                    windowsOperation(null, null, data);
                                }
                            })
                        }
                    });
                } else {
                    JxEmpTableInit("/jxEmp/selectJxEmp")
                }
            }else {
                topCommonMessage('选择您想要删除的员工！');
            }

        }

        //表中组织、部门、岗位的刷新显示方法
        function jxEmpTableEdit(index,row) {
            //部门的jquery对象
            var dept = $('#jxEmpTable').datagrid("getEditor", {index: index, field: 'pkDept'}).target;
            //用户组织
            var pkOrg = $('#jxEmpTable').datagrid("getEditor", {index: index, field: 'pkOrg'}).target;
            var post = $('#jxEmpTable').datagrid('getEditor',{index:index,field:'postId'}).target;
            //所属组长、科长、主管
            var empZuzhang = $('#jxEmpTable').datagrid('getEditor',{index:index,field:'empZuzhang'}).target;
            var empZhuguan = $('#jxEmpTable').datagrid('getEditor',{index:index,field:'empZhuguan'}).target;
            var empKezhang = $('#jxEmpTable').datagrid('getEditor',{index:index,field:'empKezhang'}).target;
            pkOrg.combobox({
                data:[{pkOrg:ORG_HUIZHOU_CODE,name:ORG_HUIZHOU_NAME},{pkOrg:ORG_ZHAOTING_CODE,name:ORG_ZHAOTING_NAME}],
                onChange: function (newOrg, oldOrg) { //组织刷新部门
                    if (newOrg) {
                        dept.combobox({
                            url: '/jxEmp/getJxDeptByPkOrg?pkOrg=' + newOrg,
                            required: true,editable:true, valueField: 'pkDept', textField: 'name',panelHeight:'auto',
                            onLoadSuccess: function (data) {
                                console.log("212312",data)
                                dept.combobox("setValue",row.pkDept) //使用此方式赋值，是让本身onChange事件继续触发
                            },
                            onChange: function (newDept, oldDept) { // 部门刷新岗位
                                console.log("212312",newDept)
                                if (newDept){
                                    post.combobox({
                                        url:'/jxEmp/getJxEmpPost?pkOrg='+newOrg+'&pkDept='+newDept,
                                        onLoadSuccess: function (node, data) {
                                            post.combobox("setValue",row.postId)
                                        },
                                        onChange: function (newPost, oldPost) { //部门刷新所属科长、所属组长、所属主管
                                            let empZuzhangData=commonAjax("get",'/jxEmp/selectEmpByLikePostName?postName=组长&pkDept='+newDept);
                                            let empZhuguanData=commonAjax("get",'/jxEmp/selectEmpByLikePostName?postName=主管&pkDept='+newDept);
                                            let empKezhangData=commonAjax("get",'/jxEmp/selectEmpByLikePostName?postName=科长&pkDept='+newDept);
                                            var aCloumns=[[
                                                {field:"empCode",title:"工号",width:"30%"},
                                                {field:"empName",title:"姓名",width:"30%"},
                                                {field:"postName",title:"岗位",width:"40%"},
                                            ]]
                                            //初始化表
                                            empZuzhang.combogrid({
                                                scrollbarSize:0,
                                                panelWidth:180,
                                                textField:'empName',
                                                idField:'empCode',
                                                columns:aCloumns,
                                                onLoadSuccess: function () {//数据加载成功后触发
                                                    empZuzhang.combogrid("setValue", row.empZuzhang)
                                                },
                                                data:empZuzhangData,
                                            })
                                            empZhuguan.combogrid({
                                                scrollbarSize:0,
                                                panelWidth:180,
                                                textField:'empName',
                                                idField:'empCode',
                                                columns:aCloumns,
                                                data:empZhuguanData,
                                                onLoadSuccess: function () {//数据加载成功后触发
                                                    empZhuguan.combogrid("setValue", row.empZhuguan)
                                                }
                                            })
                                            empKezhang.combogrid({
                                                scrollbarSize:0,
                                                panelWidth:180,
                                                textField:'empName',
                                                idField:'empCode',
                                                columns:aCloumns,
                                                data:empKezhangData,
                                                onLoadSuccess: function () {//数据加载成功后触发
                                                    empKezhang.combogrid("setValue", row.empKezhang)
                                                },
                                            })

                                            let  flag=true;
                                            //根据条件禁用组件
                                            for (let a of empKezhangData){//科长
                                                if (a.postId==newPost){
                                                    empKezhang.combogrid("setValue","");
                                                    empZuzhang.combogrid("setValue","");
                                                    empZhuguan.combogrid("setValue","");
                                                    empZuzhang.combogrid("disable");
                                                    empZhuguan.combogrid("disable");
                                                    empKezhang.combogrid("disable");
                                                    flag=false;
                                                    return
                                                }
                                            }

                                            for (let a of empZhuguanData){//主管
                                                if (a.postId==newPost){
                                                    empZhuguan.combogrid("setValue","");
                                                    empZuzhang.combogrid("setValue","");
                                                    empZuzhang.combogrid("disable");
                                                    empZhuguan.combogrid("disable")
                                                    empKezhang.combogrid("enable")
                                                    flag=false;
                                                    return
                                                }
                                            }

                                            for (let a of empZuzhangData){//组长
                                                if (a.postId==newPost){
                                                    empZuzhang.combogrid("setValue","");
                                                    empZuzhang.combogrid("disable");
                                                    empZhuguan.combogrid("enable");
                                                    empKezhang.combogrid("enable");
                                                    flag=false;
                                                    return
                                                }
                                            }

                                            if (flag){
                                                empZuzhang.combogrid("enable")
                                                empZhuguan.combogrid("enable")
                                                empKezhang.combogrid("enable")
                                            }

                                        }
                                    })
                                }
                            }
                        })
                    }

                },
                onLoadSuccess: function (node, data) {//数据加载成功后触发
                    pkOrg.combobox("setValue", row.pkOrg)//设置默认值
                }
            })

        }
        //部门格式化
        function jxEmpPkDeptFormatter(value, row, index){
            var resultDept = "";
            pkDeptAll.forEach(function (val) {
                if (val.pkDept == value) {
                    resultDept = val.name
                    return;
                }
            })
            return resultDept;
        }

//查询中的组织内容更改事件
        $("#jxEmpFormPkDept").combobox({
            data:pkDeptAll,
            textField:"name",
            valueField:"pkDept",
            onChange:function (newDept,oldDept) {
                var pkOrg=$("#jxEmpFormPkOrg").combobox("getValue");

                $("#jxEmpFormPkPost").combobox({
                    valueField: 'id',
                    textField: 'name',
                    panelWidth:120,
                    url:'/jxEmp/getJxEmpPost?pkOrg='+pkOrg+'&pkDept='+newDept,
                })
            }
        })
        //格式化所属组长、所属科长、所属主管
        function jxEmpSsonformatter(value,row,index) {
            for (var e of empAll){
                if (e.empCode==value){
                    return e.empName;
                }
            }
        }
    </script>
</div>